//
// Created by y on 2017-12-1.
//

#ifndef PLAYER_FFMPEG_H
#define PLAYER_FFMPEG_H

#include "Log.h"
#include "pthread.h"
#include "BasePlayer.h"
#include "JavaCall.h"
#include "Audio.h"
#include "Video.h"
#include "PlayStatus.h"
#include "AudioChannel.h"

extern "C"
{
#include <libavformat/avformat.h>
}

class FFmpegPlayer {

public:
    const char *urlpath = NULL;
    JavaCallFunc *JavaCall = NULL;
    pthread_t decodThread;
    AVFormatContext *pFormatCtx = NULL;//封装格式上下文
    int duration = 0;
    AudioPlayer *Audio = NULL;
    VideoPlayer *Video = NULL;
    WlPlayStatus *PlayStatus = NULL;
    bool exit = false;
    bool exitByUser = false;
    int mimeType = 1;
    bool isavi = false;
    bool isOnlyMusic = false;

    std::deque<AudioChannel*> audiochannels;
    std::deque<AudioChannel*> videochannels;

    pthread_mutex_t init_mutex;
    pthread_mutex_t seek_mutex;
public:
    FFmpegPlayer(JavaCallFunc *javaCall, const char *urlpath, bool onlymusic);
    ~FFmpegPlayer();
    int preparedFFmpeg();
    int decodeFFmpeg();
    int start();
    int seek(int64_t sec);
    int getDuration();
    int getAvCodecContext(AVCodecParameters * parameters, BasePlayer *BasePlayer);
    void release();
    void pause();
    void resume();
    int getMimeType(const char* codecName);
    void setAudioChannel(int id);
    void setVideoChannel(int id);
    int getAudioChannels();
    int getVideoWidth();
    int getVideoHeight();
};
#endif //PLAYER_FFMPEG_H
